package com.thti.mecool.client.activity;

import java.util.List;

import android.app.NotificationManager;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.thti.mecool.client.adapter.UserCenterMsgItemAdapter;
import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.send.system.GetSend;
import com.thti.mecool.client.model.biz.MessagesBizImpl;
import com.thti.mecool.client.model.po.MobileMessages;
import com.thti.mecool.client.widget.Toolbar;

public class UserCenterMsgListActivity extends BaseActivity {
	private ListView listView;
	MessagesBizImpl messagesBizImpl;
	List<MobileMessages> mobleList;
	UserCenterMsgItemAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.noTitle();
		setContentView(R.layout.mecool_usercenter_msg_list);
		Cache.add(this);
		initToolbar();
		init();
		getDataFromDb();
		service();
	}
	
	private void init(){
		listView=(ListView)findViewById(R.id.mecool_usercenter_msg_list_listview);
		messagesBizImpl=new MessagesBizImpl(this);
		NotificationManager notificationManager=(NotificationManager)this.getSystemService("notification");
		notificationManager.cancel(0);
		notificationManager.cancelAll();
//		Intent intent=new Intent(UserCenterMsgListActivity.this,ImportMsgDialog.class);
//		intent.putExtra("content","买一送一活动 发送时间:2012-5-16 18:00");
//		startActivity(intent);
	}
	
	private void getDataFromDb(){
		mobleList=messagesBizImpl.findall();
		adapter=new UserCenterMsgItemAdapter(this, mobleList);
	}
	
	private void service(){
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(listListener);
		listView.setOnItemLongClickListener(itemLongClickListener);
	}
	
	OnItemClickListener listListener=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			if(mobleList.get(arg2).getType().equals("UNREAD")||mobleList.get(arg2).getType().equals("unread")){
				GetSend.getMessage(mobleList.get(arg2).getId());
			}	
			messagesBizImpl.update(mobleList.get(arg2).getId());
			mobleList.get(arg2).setType("READ");
			adapter.notifyDataSetChanged();
			adapter=new UserCenterMsgItemAdapter(UserCenterMsgListActivity.this, mobleList);
			listView.setAdapter(adapter);
			Intent intent = new Intent(UserCenterMsgListActivity.this,UsercenterShowMsgActivity.class);
//			intent.putExtra("id", mobleList.get(arg2).getId());
			intent.putExtra("body", mobleList.get(arg2).getBody());
			startActivity(intent);
		}
	};
	
	OnItemLongClickListener itemLongClickListener=new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				final int arg2, long arg3) {
			// TODO Auto-generated method stub
			Builder builder = new Builder(UserCenterMsgListActivity.this);
			builder.setMessage("您确定要删除吗?");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					messagesBizImpl.delete(mobleList.get(arg2).getId());
					mobleList.remove(arg2);
					adapter.notifyDataSetChanged();
					adapter=new UserCenterMsgItemAdapter(UserCenterMsgListActivity.this, mobleList);
					listView.setAdapter(adapter);
				}
			})
					.setNegativeButton("取消", new DialogInterface.OnClickListener(){

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							
						}
						
					});
			builder.create();
			builder.show();	
			return false;
		}
	};
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		Toolbar.hideRightButton();
		toolbar.setToolbarCentreText("消息");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
		}};
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mobleList=messagesBizImpl.findall();
		adapter.notifyDataSetChanged();
		adapter=new UserCenterMsgItemAdapter(UserCenterMsgListActivity.this, mobleList);
		listView.setAdapter(adapter);
	}
	
	
	
	
	
}
